import requestUrl from '../../utils/requestUtil.js'
import area from '../../utils/area.js'
const QRCode = require("../../utils/weapp-qrcode.js")
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [{
            url: '../../images/erCode.png',
            title: '二维码'
        }, {
            url: '../../images/diary.png',
            title: '种植日记'
        }, ],
        //是否认证
        realNameAuth: false,
        //是否查询到产品信息，
        productAuth: false,
        showCode: false,
        isShowPoint: false,
        allProduct: [

        ],
        //展开农作物信息
        showList: false,
        userInfo: {},
        checked: false,
        pointlist: [],
        radio: 0,
        skuNo: '',
        timestamp: '',

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // wx.getStorage({
        //     key: 'token'
        //   }).then(data => {
        //     //能在storage中取到值

        //   }).catch(err => {
        //     //storage取不到值
        //     wx.navigateTo({
        //       url: '../login/login',
        //     })
        //   })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        var usertoken = wx.getStorageSync('token')
        if (usertoken) {
            this.getPoint();
            this.requestUserInfo(usertoken);
            this.requestCropsInfo(usertoken);
        }

    },

    requestUserInfo(usertoken) {
        let that = this;
        requestUrl.requestUrl({
            url: "/app/ph/info",
            params: {},
            method: "get",
            header: {
                token: usertoken
            }
        }).then((data) => {
            if (data.data.code == 0) {
                that.setData({
                    userInfo: data.data.data,
                    realNameAuth: true
                })
            } else if (data.data.code == 403) {
                that.setData({
                    realNameAuth: false,
                })
                // wx.navigateTo({
                //     url: '../login/login'
                // })
            }


        }).catch((errorMsg) => {

        })
    },
    requestCropsInfo(usertoken) {
        let that = this;
        requestUrl.requestUrl({
            url: "/app/ph/crops/list",
            params: {},
            method: "get",
            header: {
                token: usertoken
            }
        }).then((data) => {
            if (data.data.list.length > 0) {
                let datalist = data.data.list;

                let allProduct = [];

                datalist.forEach(item => {
                    let allProductdict = {};

                    let productList = [];
                    for (let i in item) {
                        let a = item[i];
                        if (a != null && a != '' && i != 'saleChannel' && i != 'id') {
                            productList.push({
                                name: i,
                                value: item[i]

                            });
                        }
                    }

                    allProductdict.productList = productList;
                    allProductdict.show = false;
                    allProductdict.skuName = item.skuName;
                    allProductdict.id = item.id;
                    allProductdict.skuNo = item.skuNo;
                    allProduct.push(allProductdict);
                })
                that.setData({
                    productAuth: true,
                    allProduct: allProduct
                })
            } else {
                that.setData({
                    productAuth: false,

                })
            }
        }).catch((errorMsg) => {

        })
    },

    goToaddCropsInfo() {
        wx.navigateTo({
            url: '../register/register',
        })
    },
    clickItem: function (e) {
        let id = e.currentTarget.dataset.index;
        let show = this.data.allProduct[id].show;
        this.setData({
            [`allProduct[${id}].show`]: !show
        })

    },
    toAuth: function (e) {
        var token = wx.getStorageSync('token')
        if (token) {
            wx.navigateTo({
                url: '../planting_increase/planting_increase',
            })
        } else {
            wx.navigateTo({
                url: '../login/login'
            })
        }
    },
    mathChangeDate(times) {
        var date = new Date(times);
        var year = date.getFullYear(); //年份
        var month = date.getMonth() + 1; //月份
        var day = date.getDate(); //日
        return year + "年" + month + "月" + day + "日";
    },
    goCode(e) {


        let coNo = this.data.pointlist[this.data.radio].userId;
        let prodNo = this.data.skuNo;

        var timestamp = Date.parse(new Date());
        timestamp = timestamp / 1000;
        let idzhi = timestamp + coNo;
        this.setData({
            timestamp: timestamp
        })
        this.getCode({
            'coNo': coNo,
            'prodNo': prodNo
        }, idzhi);
    },
    goQcode(e) {
        let index = e.currentTarget.dataset.index;

        let prodNo = e.currentTarget.dataset.item;
        if (index == 0) { //二维码
            this.setData({
                skuNo: prodNo,
                isShowPoint: true,

            })
            // that.setData({

            //     pointlist: datazhi
            // })
            // this.getPoint();
        } else {
            //日记
            wx.showToast({
                title: "敬请期待",
                icon: 'none'
            })
            wx.switchTab({
                url: '../sapling/sapling',
            })
        }
    },
    cancel() {
        this.setData({
            showCode: false
        })
    },
    onClose() {
        this.setData({

            isShowPoint: false
        })
    },
    onChange(e) {
        this.setData({
            radio: e.detail,
        });
    },
    getPoint() {

        let that = this;
        requestUrl.requestUrl({
            url: "/app/gys/list",
            params: {},
            method: "post",
            header: {
                token: wx.getStorageSync('token')
            }
        }).then((data) => {
            if (data.data.list.length > 0) {
                let datazhi = data.data.list;

                that.setData({

                    pointlist: datazhi
                })
            } else {}
        }).catch((errorMsg) => {

        })
    },

    getCode(dict, id) {

        let that = this;
        that.setData({

            isShowPoint: false
        })
        requestUrl.requestUrl({
            url: "/app/gys/qrcode",
            params: dict,
            method: "post",
            header: {
                token: wx.getStorageSync('token')
            }
        }).then((data) => {
            if (data.data.url != null) {
                let datazhi = data.data.url;
                that.setData({
                    showCode: true,

                })

                let a = new QRCode(id, {
                    text: datazhi,
                    width: 250,
                    height: 250,
                    correctLevel: QRCode.CorrectLevel.Q, // 二维码可辨识度
                    callback: (res) => {

                    }
                })



            } else {
                wx.showToast({
                    title: '当前点位异常，请稍后尝试！',
                    icon: 'none'
                })
            }
        }).catch((errorMsg) => {

        })
    },



})